/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package izsla.beans;

import izsla.entities.SearchCriteria;
import izsla.entities.Trip;
import izsla.sb.TripManagementLocal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import javax.ejb.EJB;

/**
 *
 * @author bence
 */
public class Rss {

    @EJB
    private TripManagementLocal tm;
    private SearchCriteria searchSettings = new SearchCriteria();
    private String link = "http://localhost:8080/Izsla-war/login.jsp";

    public Rss() {
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    /**
     * Helper function that converts a date to a string.
     * @param date
     */
    private String getDate(Date date) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            return cal.get(Calendar.DAY_OF_MONTH) + ". " + cal.get(Calendar.MONTH) + ". " + cal.get(Calendar.YEAR) + ".";
    }

    /**
     *
     * @return The content of the RSS page.
     */
    public String getRssContent() {
        String triprss = "";
        List<Trip> trips = tm.getTrips(searchSettings);
        System.out.println("Number of trips: " + trips.size());
        int max = trips.size() > 10 ? 10 : trips.size();
        for(int i = trips.size()-1; (i >= trips.size()-10) && (i >= 0); i--) {
            Trip t = trips.get(i);
            triprss = triprss + "<item>\n";
            triprss = triprss + "<title>" +
                    t.getDescription() +
                    " from " + getDate(t.getStartTime()) +
                    " to " + getDate(t.getEndTime()) +
                    " (" + t.getParticipants().size() + "/" + t.getLimit() + ")" +
                    "</title>\n";
            triprss = triprss + "<author>" + t.getOrganiser().getName() + "</author>\n";
            triprss = triprss + "<link>" + link + "</link>\n";
            triprss = triprss + "</item>\n";
        }
        return triprss;
    }

}
